package com.emiage.ddutils;

import android.app.Activity;
import android.content.Context;
import android.os.IBinder;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

/**
 * Created by LiJZ on 2016/7/22.
 */
public class KeyBoardUtils {
	/**
	 * 显示软键盘
	 * @param activity
	 * @return
	 */
	public static boolean showSoftInput(Activity activity) {
		View view = activity.getCurrentFocus();
		if (view != null) {
			InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(
					Context.INPUT_METHOD_SERVICE);
			return imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
		}
		return false;
	}


	public void hideSoftInput(Activity activity) {
		try {
			InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
			if (imm != null) {
				// 当前焦点view
				View localView = activity.getCurrentFocus();
				if (localView != null) {
					IBinder localIBinder = localView.getWindowToken();
					if (localIBinder != null)
						imm.hideSoftInputFromWindow(localIBinder, 0);
				}
			}
		} catch (Exception e) {
		}
	}
}
